package crivia.ecp.common;

import java.lang.reflect.InvocationTargetException;
import java.util.Enumeration;

import weaver.file.FileUpload;
import weaver.file.FileUploadToPath;
import crivia.c.CarryIO;
import crivia.mvc.RequestSender;
import crivia.mvc.i.Executer;

public class FileRecever {

	public static void rec(Executer executer, FileUpload request) {
		@SuppressWarnings("unchecked")
		Enumeration<String> paramKeys = request.getParameterNames();
		while (paramKeys.hasMoreElements()){
			String key = paramKeys.nextElement();
			String[] valueObject = request.getParameterValues(key);
			if (null == valueObject){
				continue;
			}
			if (valueObject.length > 1){
				pageDataReady(executer, key, valueObject);
			} else if (valueObject.length > 0) {
				if (! pageDataReady(executer, key, valueObject[0]))
					pageDataReady(executer, key, valueObject);
			}
		}
	}

	public static void rec(Executer executer, FileUploadToPath request) {
		@SuppressWarnings("unchecked")
		Enumeration<String> paramKeys = request.getParameterNames();
		while (paramKeys.hasMoreElements()){
			String key = paramKeys.nextElement();
			String[] valueObject = request.getParameters(key);
			if (null == valueObject){
				continue;
			}
			if (valueObject.length > 1){
				pageDataReady(executer, key, valueObject);
			} else if (valueObject.length > 0) {
				if (! pageDataReady(executer, key, valueObject[0]))
					pageDataReady(executer, key, valueObject);
			}
		}
	}
	
	private static String setter(String key){
		return "set" + key.substring(0,1).toUpperCase() + key.substring(1);
	}
	
	private static String getter(String key){
		return "get" + key.substring(0,1).toUpperCase() + key.substring(1);
	}
	
	private static String cutBack(String key){
		if (key.endsWith("_back")){
			return key.substring(0,key.length() - 5);
		}
		return key;
	}
	
	private static boolean pageDataReady(Executer executer, String key, Object valueObject){
		boolean isSuccess = true;
		key = key.replaceAll("-", ".");
		String[] keys = key == null ? null : key.split("[.]");
		if (keys == null || keys.length < 1){
			isSuccess = false;
		} else if (keys.length == 1){
			try {
				CarryIO.setterRunner(executer, setter(keys[0]), valueObject);
			} catch (Exception e) {
				isSuccess = false;
			}
		} else {
			String[] getters = new String[keys.length - 1];
			String[] setters = new String[keys.length - 1];
			for (int k = 0; k < keys.length - 1; k++){
				getters[k] = getter(keys[k]);
				setters[k] = setter(keys[k]);
			}
			String setter = setter(keys[keys.length - 1]);
			try {
				CarryIO.subCarryBuilder(executer, getters, setters, setter, valueObject);
			} catch (InvocationTargetException e) {
				if (RequestSender.pageDataLog){
					e.getTargetException().printStackTrace();
				}
				isSuccess = false;
			} catch (Exception e) {
				if (RequestSender.pageDataLog){
					e.printStackTrace();
				}
				isSuccess = false;
			}
		}
		if (! isSuccess && key.endsWith("_back")){
			isSuccess = pageDataReady(executer, cutBack(key), valueObject);
		}
		return isSuccess;
	}

}
